package com.aricksm.xuan.activaty02;

import android.app.Activity;
import android.app.Notification;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.util.Log;
import android.widget.Toast;

import java.io.IOException;
import java.io.InputStream;
import java.net.HttpURLConnection;
import java.net.URL;
import java.net.URLEncoder;

import okhttp3.OkHttpClient;
import okhttp3.Request;
import okhttp3.Response;

/**
 * Created by xuan on 16/6/24.
 */
public class CalacActivity extends Activity {

    Handler handler = new Handler(){
        @Override
        public void handleMessage(Message msg) {
            Toast.makeText(CalacActivity.this, (String)msg.obj, Toast.LENGTH_SHORT).show();
        }
    };

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.calac);



        Log.e("ni mabi ","wo bugan le");

        //LoginNet.doLogin();

        doGetTest();
       //doGet();

        // 开启一个子线程，进行网络操作，等待有返回结果，使用handler通知UI
        //new Thread(networkTask).start();

    }

    /**
     * 网络操作相关的子线程
     */
    Runnable networkTask = new Runnable() {

        @Override
        public void run() {
            // TODO
            // 在这里进行 http request.网络请求相关操作

            OkHttpClient client = new OkHttpClient();

            Request request = new Request.Builder().url("http://baidu.com/").build();

            try {
                Response response = client.newCall(request).execute();

                if(response.isSuccessful()){
                    Message msg = handler.obtainMessage();
                    msg.obj = response.body().toString();
                    handler.sendMessage(msg);

                    Log.e("net   :::",response.body().toString());
                }

            } catch (IOException e) {
                e.printStackTrace();
            }



            Message msg = new Message();
            Bundle data = new Bundle();
            data.putString("value", "请求结果");
            //msg.setData(data);
            msg.obj = "ddddddd";
            handler.sendMessage(msg);
        }
    };


    private void doGet(){

        Thread t = new Thread(){
            @Override
            public void run() {
                OkHttpClient client = new OkHttpClient();

                Request request = new Request.Builder().url("http://baidu.com/").build();

                try {
                    Response response = client.newCall(request).execute();

                    if(response.isSuccessful()){
                        Message msg = handler.obtainMessage();
                        msg.obj = response.body().toString();
                        handler.sendMessage(msg);

                        Log.e("net   :::",response.body().toString());
                    }

                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
        };

        t.run();

    }

    void doGetTest(){
        Thread t = new Thread(){
            @Override
            public void run() {
                @SuppressWarnings("deprecation")
                String path = "http://baidu.com/Web2/servlet/Login?name=" + URLEncoder.encode("dd") + //对要提交的表单数据进行url编码
                        "&pass=" + "fff";
                try {
                    URL url = new URL(path);
                    HttpURLConnection conn = (HttpURLConnection) url.openConnection();

                    conn.setRequestMethod("GET");
                    conn.setConnectTimeout(8000);
                    conn.setReadTimeout(8000);

                    if(conn.getResponseCode() == 200){
                        InputStream is = conn.getInputStream();
                        String text = is.toString();
                        Log.e("net :",text);

                        Message msg = handler.obtainMessage();
                        msg.obj = text;
                        handler.sendMessage(msg);
                    }
                } catch (Exception e) {
                    // TODO Auto-generated catch block
                    e.printStackTrace();
                }

            }
        };
        t.start();
    }



}
